<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/classes/rss/rss10.class.php, $Author:  $ */


class RSS {

    var $_aChannel;

    var $_aImage;

    var $_aItems;

    var $charset;


	function RSS () {
		$this->_aItems = array();
	}

	/** Set channel information
	 * @param string $sTitle  channel's title
	 * @param string $sDescr  channel's descripton
	 * @param string $sLink   channel's link
	 * @param string $sAbout  channel's url (url of RSS)
	 * @param array  $aParams additional parameters for different modules:
	 *                       1 Syndication - 'sy:updatePeriod', 'sy:updateFrequency', 'sy:updateBase'
	 */
	function setChannel($sTitle, $sDescr, $sLink, $sAbout, $aParams=array()) {
        $this->_aChannel['title']  = $sTitle;
        $this->_aChannel['descr']  = $sDescr;
        $this->_aChannel['link']   = $sLink;
        $this->_aChannel['about']  = $sAbout;
	    $this->_aChannel['params'] = $aParams;
	}

	/** Set image for channel
	 * @param array $image image info:
	 *           1. title - image title
	 *           3. url   - url of image file
	 *           2. link  - url for image href
	 */
	function setImage ($image) {
		$this->_aImage = array (
			'title' => $image['title'],
			'link' => $image['link'],
			'url' => $image['url']
		);
	}

    /** Add items to feed. For imte format - see addItem().
     * @param array $aItems items info (array of info arrays)
     * @see addItem()
     */
	function addData($aItems) {
		foreach($aItems as $aItem) {
			array_push($this->_aItems, $aItem);
		}
	}

	/** Add single item to feed.
	 * @param array $aItem single item info:
	 *         1. title   - item title
	 *         2. descr   - item description
	 *         3. link    - link for item full version
	 *         4. dc:...  - [optional] Dublin Core module params (see http://dublincore.org/documents/1999/07/02/dces/)
	 * @see addData()
	 */
	function addItem ($aItem) {
		array_push($this->_aItems, $aItem);
	}

	/** Get source XML-code for RSS.
	 * @return string XML-code
	 * @see send()
	 */
	function get() {
		$oReq = App::getRequest();
		$aUrl = array();
		$aUrl['act'] = $oReq->get('act');
		if ($iId = $oReq->get('id'))
		{
			$aUrl['id'] = $iId;			
		}		
		$aUrl['rss'] = $oReq->get('rss');
		if ($iUserId = $oReq->get('userid'))
		{
			$aUrl['userid'] = $iUserId;			
		}			
		header('Content-type: text/xml; charset='. $this->charset .'');
		$doc = '<?xml version="1.0" encoding="'. $this->charset .'"?>'."\n";
		$doc .= '<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom">';
		$doc .= "\n<channel>\n";
		$doc .= "\t" . '<atom:link href="' . App::makeUrl('public.rss', $aUrl) . '" rel="self" type="application/rss+xml" />' . "\n";
		$doc .= $this->_getChannel();
		$doc .= $this->_getImage();
		$doc .= $this->_getItems();
		$doc .= "</channel>\n</rss>";
		return $doc;
	}

	/** Send generated XML-code to output.
	 * @see get()
	 */
	function send()
	{
	    header('Content-type: text/xml; charset=utf-8');
	    echo $this->get();
	    exit();
	}

	/** Create channel part of code
	 * @return string source for channel part of RSS.
	 */
	function _getChannel() {
		// start channel info
		$sChannel =
			"\t" . '<title>'.$this->_escape($this->_aChannel['title']).'</title> '."\n".
			"\t" . '<link>'.$this->_aChannel['link'].'</link> '."\n".
			"\t" . '<description>'.$this->_escape($this->_aChannel['descr']).'</description>'."\n";

        //additional parameters (syndication etc.)
		foreach ($this->_aChannel['params'] as $k=>$v)
		  $sChannel .= '    <'.$k.'>'.$v.'</'.$k.'>'."\n";
		return $sChannel;
	}

	/** Create image part of code
	 * @return string source for image part of RSS.
	 */
	function _getImage() {
		if (isset($this->_aImage)) {
			$image =
				'  <image>'."\n".
				'    <title>'.$this->_escape($this->_aImage['title']).'</title>'."\n".
				'    <link>'.$this->_aImage['link'].'</link>'."\n".
				'    <url>'.$this->_aImage['url'].'</url>'."\n".
				'  </image>'."\n\n";
			return $image;
		} else {
			return "";
		}
	}

	/** Create code for items
	 * @return string source  code for items
	 */
	function _getItems() {
		$sItems = "";
		foreach ($this->_aItems as $aItem) {
			$sItems .= "\t" . '<item>'."\n".
			           "\t\t" . '<title><![CDATA['. $aItem['title'].']]></title>'."\n".
                       "\t\t" . '<link>'.$aItem['link'].'</link>'."\n".
                       "\t\t" . '<description><![CDATA['. $aItem['description'] .']]></description>'."\n";
            foreach($aItem as $k=>$v) //process additionla parameters
            {
                if (! in_array($k, array('link', 'title', 'description')))
                {
                    $sItems .= "\t\t" . '<'.$k.'>'.$this->_escape($v).'</'.$k.'>';
                }
            }
            $sItems .= "\n\t</item>\n";
		}
		return $sItems;
	}

	/** Enclode string in <![CDATA[ ... ]]> if needed
	 * @param string $sStr source string
	 * @return string enclosed string
	 */
	function _escape($sStr)
	{
	    if ( (strpos($sStr,'>')!==false) ||
	         (strpos($sStr,'<')!==false) ||
	         (strpos($sStr,'"')!==false) )
    	    return '<![CDATA['.$sStr.']]>';
    	else
    	    return $sStr;
	}
}

?>